package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(UserLoginParam userLoginParam){
        log.debug("用户登录,参数: {} ",userLoginParam);
        return JsonResult.ok(userService.login(userLoginParam));
    }

    @ApiOperation("保存用户")
    @PostMapping("/save")
    public JsonResult saveUser(UserSaveParam userSaveParam){
        log.debug("保存用户,参数: {} ",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @ApiOperation("查询用户")
    @GetMapping("/select")
    public JsonResult selectUser(UserQuery userQuery){
        log.debug("查询用户,参数: {} ",userQuery);
        List<UserVO> list = userService.selectUser(userQuery);
        return JsonResult.ok(list);
    }

    @ApiOperation("重置密码")
    @PostMapping("/update/password/{userId}")
    public JsonResult resetPassword(@PathVariable Long userId){
        log.debug("重置密码参数:userId={}",userId);
        userService.resetPassword(userId);
        return JsonResult.ok();
    }

    @ApiOperation("更改用户状态信息")
    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult changeStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("更改状态:userId={},status={}",userId,status);
        userService.changeStatus(userId,status);
        return JsonResult.ok();
    }

    @ApiOperation("删除用户功能")
    @PostMapping("/delete/{userId}")
    public JsonResult deleteUser(@PathVariable Long userId){
        log.debug("删除用户Param: userId={} ",userId);
        userService.deleteUserById(userId);
        return JsonResult.ok();
    }

    @GetMapping("/select/audit/{parentId}")
    public JsonResult selectAudit(@PathVariable Long parentId){
        log.debug("加载审批人方法:parentId={} ",parentId);
        List<UserVO> list = userService.selectAuditList(parentId);
        return JsonResult.ok(list);
    }

}
